package com.example.rabbitmq_study.topic;

import com.example.rabbitmq_study.util.RabbitmqUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author Huangyt
 * @version 1.0
 * @date 2020/7/29 10:06
 */
public class Provider {
    public static void main(String[] args) throws IOException, TimeoutException {
        //获取连接对象
        Connection connection = RabbitmqUtil.getConnection();

        //创建通道
        Channel channel = connection.createChannel();

        /*
            声明交换机
                参数1：交换机名称
                参数2：交换机类型 —— 通配符模式
         */
        channel.exchangeDeclare("topics", "topic");

        //路由key
        String routingKey = "user.ab";

        //发送消息
        channel.basicPublish("topics", routingKey, MessageProperties.PERSISTENT_BASIC, ("这是topic模式基于routing key:[" + routingKey + "] 发送的消息").getBytes());

        //关闭通道、连接
        RabbitmqUtil.close(channel, connection);
    }
}
